package sui.core
{
    import flash.display.Sprite;
    import flash.events.Event;

    import sui.utils.NameUtil;

    internal class ComponentBase extends Sprite
    {
        private var _invalid:Boolean;

        public function ComponentBase()
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            addEventListener(Event.RENDER, renderHandler);
        }

        public function invalidate():void
        {
            _invalid = true;
            if (stage)
            {
                stage.invalidate();
            }
        }

        private function addedToStageHandler(event:Event):void
        {
            removeEventListener(event.type, arguments.callee);
            invalidate();
        }

        protected function renderHandler(event:Event):void
        {
            // TODO Auto-generated method stub
            if (_invalid)
            {
                doRefresh();
                _invalid = true;
            }
        }


        protected function doRefresh():void
        {
        }

        override public function toString():String
        {
            return NameUtil.displayObjectToString(this);
        }
    }
}
